﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ValidEnumValueAttribute.cs
// 功能描述：TongTongMall.MallWeb.Application
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.MallWeb.Application.Attributes.ModelBinders
{
    public class ValidEnumValueAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var result = new ValidationResult("错误的枚举类型！");
            if (value != null)
            {
                Type enumType = value.GetType();
                bool valid = Enum.IsDefined(enumType, value);
                if (valid) return ValidationResult.Success;
            }
            return result;
        }
    }
}
